﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AForge.Imaging;
using VideoFlowStatistics.EntityClass;

namespace VideoFlowStatistics.Class
{
    class PeopleCount
    {
        //撞线位置
        private static int MinArea = 0;//最小面积
        private static int OneArea = 0;//一个人的最小面积
        private static double wTime = 0;//
        private static Dictionary<string, Pcount> pcount { get; set; }
        private static Pcount[] tmppcount { get; set; }

        private static DateTime StartTime = new DateTime();

        static PeopleCount()
        {
            if (pcount == null)
            {
                pcount = new Dictionary<string, Pcount>();
            }
            if (tmppcount == null)
            {
                tmppcount = new Pcount[] { };
            }
            
        }

        /// <summary>
        /// 处理人数
        /// 直接记录数量到文件
        /// </summary>
        public static void ProcessPCount(Blob[] recBlobItems, ref int[] count)
        {
            MinArea = Properties.Settings.Default.MinArea;//最小面积
            OneArea = Properties.Settings.Default.OneArea;//一个人的最小面积
            wTime = Properties.Settings.Default.WTime;//获取间隔时间

            int intNowTime = Convert.ToInt32(DateTime.Now.ToString("HHmmssfff"));
            int i = 0;
            foreach (var item in recBlobItems)
            {
                //最小面积过滤
                if (MinArea > 0 && item.Area < MinArea)
                {
                    continue;
                }

                if (OneArea > 0 && (item.Area / OneArea) > 1)
                {
                    i += (item.Area / OneArea);
                }
                else if (OneArea > 0 && (item.Area % OneArea) > 0)
                {
                    i++;
                }
                else
                {
                    i++;
                }

            }
            //AllCount = i;
            pcount.Add(Guid.NewGuid().ToString(), new Pcount() { LCount = i, NowTime = intNowTime });
            //每2分钟记录一次人数
            if (i > 0 && (DateTime.Compare(StartTime.AddMinutes(wTime), DateTime.Now) < 0))
            {
                XmlHelper.Insert("Time", "Count", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), i.ToString());
                StartTime = DateTime.Now;
            }
            if (pcount.Count > 1)
            {
                if ((pcount.Last().Value.NowTime - pcount.First().Value.NowTime) >= 800)//800m秒两帧显示一次
                {
                    if (tmppcount.Length == 0)
                    {
                        tmppcount = new Pcount[pcount.Values.Count];
                        count = new int[pcount.Values.Count];
                        pcount.Values.CopyTo(tmppcount, 0);
                        pcount.Clear();//清空原列表
                        float result = 0;
                        for (int k = 1; k < tmppcount.Length; k++)
                        {
                            result += Convert.ToSingle(GetAbs(tmppcount[k].LCount, tmppcount[k - 1].LCount) * 0.9);
                            count[k] = Convert.ToInt32(result);
                        }
                        tmppcount = new Pcount[] { };
                    }
                    //count = pcount.Sum(p => p.Value.LCount) / pcount.Count;

                    ////写入xml
                    //if (count > 0)
                    //{
                    //    XmlHelper.Insert("Time", "Count", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), count.ToString());
                    //}
                    //pcount.Clear();
                }
            }
            else if (i == 0)
            {
                count = new int[] { };
                tmppcount = new Pcount[] { };
                pcount.Clear();
            }
        }
        /// <summary>
        /// 画面人数处理
        /// 不直接记录数量到文件
        /// </summary>
        public static void ProcessPCountSing(Blob[] recBlobItems, ref int count)
        {
            MinArea = Properties.Settings.Default.MinArea;//最小面积
            OneArea = Properties.Settings.Default.OneArea;//一个人的最小面积
            wTime = Properties.Settings.Default.WTime;//获取间隔时间

            int intNowTime = Convert.ToInt32(DateTime.Now.ToString("HHmmssfff"));
            int i = 0;
            foreach (var item in recBlobItems)
            {
                //最小面积过滤
                if (MinArea > 0 && item.Area < MinArea)
                {
                    continue;
                }

                if (OneArea > 0 && (item.Area / OneArea) > 1)
                {
                    i += (item.Area / OneArea);
                }
                else if (OneArea > 0 && (item.Area % OneArea) > 0)
                {
                    i++;
                }
                else
                {
                    i++;
                }

            }
            count = i;
        }

        private static float GetAbs(int firstnum, int secondnum)
        {
            float abs = Math.Abs((float)firstnum + (float)secondnum);
            float result = Math.Max(firstnum, secondnum);
            if (result == 0)
                result = 1;
            return abs / result;
        }
    }
}
